<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog title="新增权限点" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="enterBtn">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // 是否显示弹层
    showDialog: {
      type: Boolean,
      default: false
    },
    // 编辑或添加标记
    isEdit: {
      type: Boolean,
      default: false
    },
    // 权限列表
    permissionList: {
      type: Array,
      default: _ => []
    },
    // 当前点击项id
    clickId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    // 自定义校验权限名称
    const validName = (rule, value, callback) => {
      // 找出和当前点击项同级的数据
      let existNameList = this.permissionList.filter(item => item.pid === this.clickId)
      // 编辑状态
      if (this.isEdit) {
        // 找到当前点击项的数据对象
        const obj = this.permissionList.find(item => item.id === this.clickId)
        // 保存pid
        const pid = obj.pid
        // 找出和当前pid相同的对象(当前的子级)并去掉当前对象
        existNameList = this.permissionList.filter(item => item.pid === pid && item.id !== this.clickId)
      }
      // 映射出数组里所有的name进行比较
      existNameList.map(i => i.name).includes(value) ? callback(new Error(value + '已经占用')) : callback()
    }
    // 自定义校验权限名称
    const validCode = (rule, value, callback) => {
      // 找出所有的权限名称
      let existCodeList = this.permissionList.map(item => item.code)
      // 编辑状态
      if (this.isEdit) {
        // 先去除自己的权限名称
        existCodeList = this.permissionList.filter(item => item.id !== this.clickId).map(item => item.code)
      }
      // 进行比较
      existCodeList.includes(value) ? callback(new Error(value + '已经占用')) : callback()
    }
    return {
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0' // 开启
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 取消按钮 -> 关闭弹窗
    btnCancel() {
      // 清空表单
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      this.$refs.perForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    // 确定按钮
    enterBtn() {
      this.$refs.perForm.validate(valid => {
        if (!valid) return
        this.$emit('confirmEv', { ...this.formData }) // 向父组件传值
        // this.btnCancel()
      })
    }
  }
}
</script>

<style>

</style>
